<?php
/**
 * Created by.
 * User: littleU-569598569
 * Date: 2021/2/1 0001
 * Time: 20:27
 */

namespace app\api\model;


class AdminMenu extends BaseModel
{
    public function pMenu()
    {
        return $this->hasOne('adminMenu','id','pid');
    }

    //得到树形结构菜单
    public function getTree($menus)
    {
        $tree = [];
        foreach ($menus as $menu)
        {
            if($menu['pid'] == 0)
            {
                $sonMenus = self::where('pid', $menu['id'])->select();
                if(count($sonMenus) > 0)
                {
                    foreach($sonMenus as $sonMenu)
                    {
                        $sunMenus = self::where('pid', $sonMenu['id'])->select();
                        $sonMenu['list'] = $sunMenus;
                    }
                }
                $menu['list'] = $sonMenus;
                array_push($tree, $menu);
            }
        }
        return $tree;
    }

}